<template>
  <div>
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="用户名">
        <el-input v-model="searchForm.username" placeholder="用户名" />
      </el-form-item>
      <el-form-item label="电话号">
        <el-input v-model="searchForm.mobile" placeholder="电话号" />
      </el-form-item>
      <el-form-item label="所属部门">
        <el-input v-model="searchForm.deptName" readonly placeholder="所属部门" @focus="deptDialog" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch()">查询</el-button>
        <el-button @click="handleEdit()">添加</el-button>
        <el-button @click="handleDeleteBatch()">批量删除</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column prop="userId" label="用户ID" width="180" />
      <el-table-column prop="username" label="用户名" width="180" />
      <el-table-column label="所属部门" width="180">
        <template slot-scope="scope">
          {{ scope.row.deptName }}
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="180" />
      <el-table-column prop="mobile" label="手机号" width="180" />
      <el-table-column prop="status" label="禁用状态" width="180">
        <template slot-scope="scope">
          {{ scope.row.status==1?'正常':'禁用' }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column
        fixed="right"
        label="操作"
        width="100"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row.userId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="current"
      :page-sizes="[10,20,30]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="400"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      title="选择部门"
      :visible.sync="deptDialogVisible"
      width="30%"
    >
      <el-tree
        ref="tree"
        :data="depts"
        node-key="deptId"
        :props="defaultProps"
        @node-click="handleNodeClick"
      />
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      depts: [],
      size: 10,
      current: 1,
      total: 0,
      multipleSelection: [],
      searchForm: {},
      title: '',
      defaultProps: {
        children: 'sysDeptList',
        label: 'name'
      },
      regions: [],
      regionsProps: {
        value: 'regionId',
        label: 'regionName',
        children: 'regions'
      },
      deptDialogVisible: false

    }
  },
  created() {
    this.initData()
    this.initDept()
    this.initRegion()
  },
  methods: {
    deptDialog() {
      this.deptDialogVisible = true
    },
    handleNodeClick(data) {
      this.searchForm.deptName = data.name
      this.searchForm.deptId = data.deptId
      this.deptDialogVisible = false
    },
    handleEdit(row) {
      if (row) {
        var userId = row.userId
      } else {
        userId = 0
      }
      this.$router.replace({ 'path': 'editSysUser' })
      localStorage.setItem('userId', userId)
    },
    handleDelete(ids) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios({
          method: 'post',
          url: 'http://localhost:9999/system-management/sysUser/delete',
          params: {
            userIds: ids
          }
        }).then((res) => {
          if (res) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.initData()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleDeleteBatch() {
      if (this.multipleSelection.length > 0) {
        var ids = this.multipleSelection.map(row => row.userId).join(',')
        this.handleDelete(ids)
      } else {
        this.$message({
          showClose: true,
          message: '请选择您要删除的数据',
          type: 'warning'
        })
      }
    },
    handleSearch() {
      this.current = 1
      this.initData()
    },
    initData() {
      this.axios({
        method: 'post',
        url: 'http://localhost:9999/system-management/sysUser/list',
        params: {
          pageNum: this.current,
          pageSize: this.size,
          username: this.searchForm.username,
          mobile: this.searchForm.mobile,
          deptName: this.searchForm.deptName
        }
      }).then((res) => {
        if (res.data.obj.list) {
          this.tableData = res.data.obj.list
          this.total = res.data.obj.total
        }
      })
    },
    initDept() {
      this.axios({
        method: 'post',
        url: 'http://localhost:9999/system-management/sysDept/list',
        params: {

        }
      }).then((res) => {
        if (res.data) {
          this.depts = res.data.obj
        }
      })
    },
    initRegion() {
      this.axios({
        method: 'post',
        url: 'http://localhost:9999/system-management/sysRegion/list',
        params: {
          parentId:1
        }
      }).then((res) => {
        if (res.data) {
          this.regions = res.data.obj
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.size = val
      this.initData()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.current = val
      this.initData()
    }
  }
}
</script>

<style scoped>

</style>
